package test.com.newproject.ui;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.percent.PercentLayoutHelper;
import android.support.percent.PercentRelativeLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import test.com.newproject.R;

/**
 * desc:
 * author：zjh
 * date:2017/12/7
 * time:10:27
 */
public class DownloadDialog extends Dialog {
    @BindView(R.id.dialog_download_title_tv)
    TextView titleTv;
    @BindView(R.id.dialog_download_tips_tv)
    TextView tipsTv;
    @BindView(R.id.dialog_download_progress_tv)
    TextView progressTv;
    @BindView(R.id.dialog_download_progress_v)
    View progressV;
    @BindView(R.id.dialog_download_background_tv)
    TextView backgroundTv;

    protected boolean fullScreen = true;
    protected View contentView;
    protected String title, tips;

    protected int force;

    public DownloadDialog(@NonNull Context context, int force) {
        super(context, R.style.FullscreenDialog);
        this.force = force;
        initView();
    }

    private void initView() {
        contentView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_download, null);
        ButterKnife.bind(this, contentView);
        setTitle(title);
        setTips(tips);
        setCancelable(false);
        if (force == 1) {
            backgroundTv.setVisibility(View.GONE);
        } else {
            backgroundTv.setVisibility(View.VISIBLE);
        }

        setContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        if (fullScreen) {
            getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        }
    }

    public void refreshProgress(final float progress) {
        contentView.post(new Runnable() {
            @Override
            public void run() {
                progressTv.setText(String.format("%d %%", (int) (progress * 100)));
                PercentLayoutHelper.PercentLayoutInfo layoutInfo = ((PercentRelativeLayout.LayoutParams) progressV.getLayoutParams()).getPercentLayoutInfo();
                layoutInfo.widthPercent = progress;
                progressV.requestLayout();
            }
        });
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        if (titleTv != null) {
            titleTv.setText(title);
        }
    }

    public String getTips() {
        return tips;
    }

    public void setTips(String tips) {
        this.tips = tips;
        if (tipsTv != null) {
            tipsTv.setText(tips);
        }
    }

    @OnClick(R.id.dialog_download_background_tv)
    public void onViewClicked() {
        ToastUtils.showLong("正在后台下载...");
        dismiss();
    }
}
